﻿using Maticsoft.DBUtility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using Xiaov.SmartWeb.Model.Statistical;

namespace Xiaov.SmartWeb.Application.PluginSrc.StatisticalSystem.OGWell
{
    public partial class Save : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string dwbm = Request.Form["dwbm"].ToRequestString();
            string year = Request.Form["year"].ToRequestString();
            string month = Request.Form["month"].ToRequestString();
            string xml = Request.Form["data"].ToRequestString();
            //string xml = "%3CDataRoot%3E%3CRecord%3E%3CNF%3E2010%3C%2FNF%3E%3CYF%3E9%3C%2FYF%3E%3CZBBM%3ESC220010%3C%2FZBBM%3E%3CDWBM%3EXB12%3C%2FDWBM%3E%3CZBMC%3E%C2%A0%C2%A0%C2%A0%EF%BC%882%EF%BC%89%E6%B3%A8%E6%B0%B4%3C%2FZBMC%3E%3CZBDM%3E%3C%2FZBDM%3E%3CCJ%3E4%3C%2FCJ%3E%3CHC%3E10%3C%2FHC%3E%3CJLDW%3E%E4%B8%87%E5%90%A8%3C%2FJLDW%3E%3CNJH%3E%3C%2FNJH%3E%3CBYSJ%3E2%3C%2FBYSJ%3E%3CBNLJ%3E%3C%2FBNLJ%3E%3CQNTQLJ%3E0%3C%2FQNTQLJ%3E%3CBZ%3E%3C%2FBZ%3E%3CLBBM%3ESC220010%3C%2FLBBM%3E%3CROWID%3EAAAmKmAAQAAACipABH%3C%2FROWID%3E%3C%2FRecord%3E%3CRecord%3E%3CNF%3E2010%3C%2FNF%3E%3CYF%3E9%3C%2FYF%3E%3CZBBM%3ESC220011%3C%2FZBBM%3E%3CDWBM%3EXB12%3C%2FDWBM%3E%3CZBMC%3E%C2%A0%C2%A0%C2%A0%EF%BC%883%EF%BC%89%E6%B2%B9%E6%B0%94%E8%BF%90%E8%BE%93%3C%2FZBMC%3E%3CZBDM%3E%3C%2FZBDM%3E%3CCJ%3E4%3C%2FCJ%3E%3CHC%3E11%3C%2FHC%3E%3CJLDW%3E%E4%B8%87%E5%90%A8%3C%2FJLDW%3E%3CNJH%3E%3C%2FNJH%3E%3CBYSJ%3E2%3C%2FBYSJ%3E%3CBNLJ%3E%3C%2FBNLJ%3E%3CQNTQLJ%3E0%3C%2FQNTQLJ%3E%3CBZ%3E%3C%2FBZ%3E%3CLBBM%3ESC220011%3C%2FLBBM%3E%3CROWID%3EAAAmKmAAQAAACipABI%3C%2FROWID%3E%3C%2FRecord%3E%3C%2FDataRoot%3E";
            string table = Request.Form["tn"].ToRequestString();
            xml = Server.UrlDecode(xml);
            try
            {
                UpdateOrInsertData(table, xml);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public void UpdateOrInsertData(string table, string xml)
        {
            //解析并保存
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            XmlNodeList nodelist = xmlDoc.DocumentElement.SelectNodes("Record");
            string sql = "begin ";

            foreach (XmlNode node in nodelist)
            {
                //这些个主键
                string jh = node.SelectSingleNode("JH").InnerText.Trim();
                string nf = node.SelectSingleNode("NF").InnerText.Trim();
                string yf = node.SelectSingleNode("YF").InnerText.Trim();
                string dw = node.SelectSingleNode("DWBM").InnerText.Trim();

                if (!string.IsNullOrEmpty(jh)&&!string.IsNullOrEmpty(nf)&&!string.IsNullOrEmpty(yf)&&!string.IsNullOrEmpty(dw))
                {
                    if (DbHelperOra.Exists("select count(1) from " + table + " where dwbm='" + dw + "' and nf=" + nf + " and yf=" + yf + " and jh='" + jh + "'"))
                    {
                        sql += " update " + table + " set ";
                        foreach (XmlElement child in node)
                        {
                            if (child.Name.ToLower() != "rowid")
                                sql += child.Name + "='" + child.InnerText.Trim() + "',";
                        }
                        sql = sql.Remove(sql.Length - 1);
                        sql += " where DWBM=" + dw.ToOracleDBString() + 
                            " and NF="+nf.ToOracleDBString()+
                            " and YF="+yf.ToOracleDBString()+
                            " and JH=" + jh.ToOracleDBString() +";";
                    }
                    else
                    {
                        string insertsql = "insert into " + table + "";
                        string t1 = "";
                        string t2 = "";
                        foreach (XmlElement child in node)
                        {
                            if (child.Name.ToLower() != "rowid")
                            {
                                t1 += child.Name + ",";
                                if (!string.IsNullOrEmpty(child.InnerText.Trim()))
                                {
                                    t2 += "'" +child.InnerText + "',";
                                }
                                else
                                    t2 += "null,";
                            }
                        }
                        t1 = t1.Remove(t1.Length - 1);
                        t2 = t2.Remove(t2.Length - 1);
                        insertsql += " ( " + t1 + " ) values (" + t2 + ");";
                        sql += insertsql;
                    }
                }
            }
            sql += "null;end;";
            DbHelperOra.ExecuteSql(sql);
        }

        ///// <summary>
        ///// 保存数据表
        ///// </summary>
        ///// <param name="table">表</param>
        ///// <param name="xml">数据xml格式</param>
        //public void SaveData(string table, string xml,string dwbm,string year,string month)
        //{

        //    //解析并保存
        //    XmlDocument xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(xml);
        //    XmlNodeList nodelist = xmlDoc.DocumentElement.SelectNodes("Record");
        //    string sql = "begin ";
        //    foreach (XmlNode node in nodelist)
        //    {
        //        string jh = node.SelectSingleNode("JH").InnerText;
        //        string bygj = node.SelectSingleNode("BYGJ").InnerText;
        //        string bysc = node.SelectSingleNode("BYSC").InnerText;
        //        string bycql = node.SelectSingleNode("BYCQL").InnerText;
        //        string bz = node.SelectSingleNode("BZ").InnerText;
        //        if (!string.IsNullOrEmpty(jh))
        //        {
        //            if (DbHelperOra.Exists("select count(1) from "+table+" where dwbm='" + dwbm + "' and nf=" + year + " and yf=" + month + " and jh='" + jh + "'"))
        //            {
        //                sql += "update " + table + " set jh='" + jh + "',";
        //                if (!string.IsNullOrEmpty(bygj))
        //                {
        //                    sql += "bygj=" + bygj + ",";
        //                }
        //                else
        //                    sql += "bygj=null,";

        //                if (!string.IsNullOrEmpty(bysc))
        //                {
        //                    sql += "bysc=" + bysc + ",";
        //                }
        //                else
        //                    sql += "bysc=null,";

        //                if (!string.IsNullOrEmpty(bycql))
        //                {
        //                    sql += "bycql=" + bycql + ",";
        //                }
        //                else
        //                    sql += "bycql=null,";

        //                if (!string.IsNullOrEmpty(bz))
        //                {
        //                    sql += "bz=" + bz + "";
        //                }
        //                else
        //                    sql += "bz=null";
        //                sql += " where nf=" + year + " and yf=" + month + " and jh=" + jh+";";
        //            }

        //            else
        //            {
        //                sql += "Insert into " + table + " (nf,yf,dwbm,jh,bygj,bysc,bycql,bz) values(" + year + "," + month + ",'" + dwbm + "', '" + jh + "',";
        //                if (!string.IsNullOrEmpty(bygj))
        //                {
        //                    sql += "" + bygj + ",";
        //                }
        //                else
        //                    sql += "null,";

        //                if (!string.IsNullOrEmpty(bysc))
        //                {
        //                    sql += "" + bysc + ",";
        //                }
        //                else
        //                    sql += "null,";

        //                if (!string.IsNullOrEmpty(bycql))
        //                {
        //                    sql += "" + bycql + ",";
        //                }
        //                else
        //                    sql += "null,";

        //                if (!string.IsNullOrEmpty(bz))
        //                {
        //                    sql += "" + bz + ");";
        //                }
        //                else
        //                    sql += "null);";
        //            }
        //        }
        //    }
        //    sql += "null;end;";
        //    DbHelperOra.ExecuteSql(sql);
        //}


       
    }
}